package com.avic.rong.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.R;


/**
 * Creater: Created by wangyz on 2017/1/18.
 * Email: 936804097@qq.com
 */

public class CompareFragment extends BaseLazyFragment{


    public static final String ARGUMENT_CAT_NAME = "authorityType";
    public static final String ARGUMENT_CAT_ID = "argument_cat_id";
    public static final String COME_FROM = "come_from";
    public static final String ARGUMENT_START_END = "start_end";
    //    private TextView tv_compare_percent;//对比百分比
//    private TextView tv_test;
//    private PullToRefreshSwipeListView recycler_view_band;
//    private List<ProductionBean> productionBeanList1 = new ArrayList<>();
//    private String comeFromSource = ConstantsUtils.EMPTY_STR;
//    private Disposable productionDisposable;
//    public List<ProductionBean> productionBeanList = new ArrayList<>();
//    public List<ProductionBean> productionBeanListForServer = new ArrayList<>();
//    public List<String> productionCheckedIds = new ArrayList<>();
//    private CompareFragmentProductionAdapter authorityEvaluatingAdapter;
//    private String categoryId;
//    private String categoryName;
//    private boolean controllSwipStartEnd = false;
//
//    //    @BindView(R.id.tv_start_compare)
////    TextView tv_start_compare;//加入对比
//    @BindView(R.id.loading_layout)
//    LoadingLayout loading_layout;
//    //    @BindView(R.id.rl_bottom)
////    RelativeLayout rl_bottom;//底部导航
//    @BindView(R.id.btn_start_compare)
//    StateButton btn_start_compare;//开始对比


    public CompareFragment() {
        super();
    }

    /**
     * 接收添加过来的prod
     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void ChangeRecyclerViewEvent(CompareFragmentChangeEvent changeEvent) {
//        if (changeEvent.getCatId().equals(categoryId)) {
//            loadDataFromStorage();
//        }
//    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_compare_production, null);

//        ButterKnife.bind(this, view);
//        Bundle tempBundle = getArguments();
//        if (tempBundle != null) {
//            categoryName = tempBundle.getString(ARGUMENT_CAT_NAME);
//            comeFromSource = tempBundle.getString(COME_FROM);
//            categoryId = tempBundle.getString(ARGUMENT_CAT_ID);
//            controllSwipStartEnd = tempBundle.getBoolean(ARGUMENT_START_END);
//        }
//
////        tv_compare_percent = (TextView) view.findViewById(R.id.tv_compare_percent);
//        recycler_view_band = (PullToRefreshSwipeListView) view.findViewWithTag("recycler_view_band");
//        recycler_view_band.getRefreshableView().setOnMenuItemClickListener(menuItemClickListener);
//        recycler_view_band.getRefreshableView().setMenuCreator(creator);
////        recycler_view_band.getRefreshableView().setInViewPagerLeftScroll();//控制viewpager中swipmenu左侧滑动
////        recycler_view_band.getRefreshableView().setSwipStartEnd(false);
//
//
//        authorityEvaluatingAdapter = new CompareFragmentProductionAdapter(getActivity(), productionBeanList1, comeFromSource);
//        authorityEvaluatingAdapter.setChangeComparePercentInterface(new CompareFragmentProductionAdapter.ChangeComparePercentInterface() {
//            @Override
//            public void changePercent(int selectNo) {//更改选中的商品比例
////                tv_compare_percent.setText(percent);
//                if (selectNo >= 2) {
//                    btn_start_compare.setPressedBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//                    btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//                    btn_start_compare.setOnClickListener(CompareFragment.this);
////                    tv_start_compare.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
////                    tv_start_compare.setOnClickListener(CompareFragment.this);
//                } else {
//                    btn_start_compare.setPressedBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//                    btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//                    btn_start_compare.setOnClickListener(null);
//                }
//            }
//        });


//        View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.footer_view, null);
//        recycler_view_band.getRefreshableView().addFooterView(footerView);
//        recycler_view_band.getRefreshableView().setAdapter(authorityEvaluatingAdapter);
//        recycler_view_band.getRefreshableView().setOnItemClickListener(authorityEvaluatingAdapter);
//        recycler_view_band.addItemDecoration(new MyDecoration(getActivity(),MyDecoration.VERTICAL_LIST));
        //加载数据
//        loadDataFromStorage();

//        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {//重新加载
//            @Override
//            public void onReload(View v) {//重新加载
//                productListSearchApi.setPageNo("0");
//                HttpManager.getInstance().doHttpDeal(productListSearchApi);
//            }
//        });

//        LoadingLayout.getConfig()
//                .setErrorText("出错啦~请稍后重试！")
////                .setEmptyText("请添加同类商品进行对比")
//                .setNoNetworkText("网络不可用，请检查后重试");
        //空页面处理
//        View emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.widget_empty_page_product, null);
//        emptyView.findViewById(R.id.ll_add_product).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onUpTimeClick(View view) {//添加商品
//                //点击对比页添加商品
//                EventLog.logFilter(ConstantsUtils.EV_TIANJIASHANGPINCLICK);
//                StatService.onEvent(getActivity(),ConstantsUtils.EV_TIANJIASHANGPINCLICK, "点击对比页添加商品");
//
//                startActivity(SameCategoryCompareActivity.getIntent(getActivity(), categoryName,
//                        categoryId, SameCategoryCompareActivity.FROM_COMPARE));
//            }
//        });
//
//        if (loading_layout != null) {
//            loading_layout.setEmptyPage(emptyView);
//            loading_layout.setLoadingPage(R.layout.widget_loading_page);
//            loading_layout.setStatus(LoadingLayout.LOADING);
//            loading_layout.setEmptyText("添加商品");
//        }
//
//        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
//            @Override
//            public void onReload(View v) {
//                loadDataFromStorage();
//            }
//        });

        return view;
    }


//    private void loadDataFromStorage() {
//
//        loading_layout.setStatus(LoadingLayout.LOADING);
//
//        productionDisposable = Flowable.create(new FlowableOnSubscribe<List<ProductionBean>>() {
//            @Override
//            public void subscribe(FlowableEmitter<List<ProductionBean>> emitter) throws Exception {
//                productionBeanList = ProductionBeanService.getInstance().queryAllProByCatId(categoryId);
//                productionCheckedIds = ProductionBeanService.getInstance().queryAllCheckedProIDsByCatId(categoryId);
//                emitter.onNext(productionBeanList);
//            }
//        }, BackpressureStrategy.DROP)
//                .subscribeOn(Schedulers.computation())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<List<ProductionBean>>() {
//                    @Override
//                    public void accept(List<ProductionBean> result) throws Exception {
//
//                        //从服务器端加载数据，为了保持评级的实时性
//                        String tempStr = ConstantsUtils.EMPTY_STR;
//                        for (int i = 0; i < productionBeanList.size(); i++) {
//                            if (i != productionBeanList.size() - 1) {
//                                tempStr = tempStr + productionBeanList.get(i).getProId() + ",";
//                            } else {
//                                tempStr = tempStr + productionBeanList.get(i).getProId();
//                            }
//                        }
//                        ProductListSearchByIds productListSearchByIds = new ProductListSearchByIds(productListListener, (RxAppCompatActivity) getActivity(), tempStr);
//                        HttpManager.getInstance().doHttpDeal(productListSearchByIds);
//
////                        loading_layout.setStatus(LoadingLayout.LOADING);
//
//
////                        int sumSelect = 0;
////                        for (ProductionBean productionBean : result) {
////                            if (productionBean.isCheck()) {
////                                sumSelect++;
////                            }
////                        }
////                        if (productionBeanList.size() > 0) {
//////                            rl_bottom.setVisibility(View.VISIBLE);
////                            btn_start_compare.setVisibility(View.VISIBLE);
////                            if (sumSelect < 2) {
////                                btn_start_compare.setPressedBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
////                                btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
////                                btn_start_compare.setOnClickListener(null);
//////                                tv_start_compare.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//////                                tv_start_compare.setOnClickListener(null);
////                            } else {
////                                btn_start_compare.setPressedBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
////                                btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
////                                btn_start_compare.setOnClickListener(CompareFragment.this);
//////                                tv_start_compare.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//////                                tv_start_compare.setOnClickListener(CompareFragment.this);
////                            }
////                        } else {
////                            btn_start_compare.setVisibility(View.GONE);
//////                            rl_bottom.setVisibility(View.GONE);
////                        }
////
//////                        tv_compare_percent.setText(sumSelect + "/4");
//////                        authorityEvaluatingAdapter.setSelectCount(sumSelect);
////                        productionBeanList = result;
////                        if (productionBeanList.isEmpty()) {
////                            if (loading_layout != null) {
////                                loading_layout.setStatus(LoadingLayout.EMPTY);
////                            }
////                            EventBus.getDefault().post(new ControlPlusVisibleEvent(false, categoryId));
////                        } else {
////                            if (loading_layout != null) {
////                                loading_layout.setStatus(LoadingLayout.SUCCESS);
////                            }
////                            EventBus.getDefault().post(new ControlPlusVisibleEvent(true, categoryId));
////                        }
////                        View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.view_empty_footer,null);
////                        recycler_view_band.getRefreshableView().addView(footerView);
////                        authorityEvaluatingAdapter.setmData(productionBeanList);
//                    }
//                });
//    }


//    HttpOnNextListener productListListener = new HttpOnNextListener<BaseResultEntity>() {
//        @Override
//        public void onError(Throwable e) {
//            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
//                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
//            } else {
//                loading_layout.setStatus(LoadingLayout.ERROR);
//            }
//        }
//
//        @Override
//        public void onNext(BaseResultEntity subjects) {
//            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_1000)) {
//                Toast toast = Toast.makeText(getActivity(), subjects.getMsg(), Toast.LENGTH_LONG);
//                toast.setGravity(Gravity.CENTER, 0, 0);
//                toast.show();
//                return;
//            }
//
//
////            loading_layout.setStatus(LoadingLayout.SUCCESS);
//            productionBeanListForServer = (List<ProductionBean>) subjects.getData();
//
//            int sumSelect = 0;
//            if (productionBeanListForServer != null) {
//                for (ProductionBean productionBean : productionBeanListForServer) {
//                    if (productionCheckedIds.contains(productionBean.getProId())) {
//                        productionBean.setCheck(true);
//                        sumSelect++;
//                    } else {
//                        productionBean.setCheck(false);
//                    }
//                }
//            }
//
//            if (productionBeanList.size() > 0) {
////                            rl_bottom.setVisibility(View.VISIBLE);
//                btn_start_compare.setVisibility(View.VISIBLE);
//                if (sumSelect < 2) {
//                    btn_start_compare.setPressedBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//                    btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//                    btn_start_compare.setOnClickListener(null);
////                                tv_start_compare.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
////                                tv_start_compare.setOnClickListener(null);
//                } else {
//                    btn_start_compare.setPressedBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//                    btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//                    btn_start_compare.setOnClickListener(CompareFragment.this);
////                                tv_start_compare.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
////                                tv_start_compare.setOnClickListener(CompareFragment.this);
//                }
//            } else {
//                btn_start_compare.setVisibility(View.GONE);
////                            rl_bottom.setVisibility(View.GONE);
//            }
//
////                        tv_compare_percent.setText(sumSelect + "/4");
////                        authorityEvaluatingAdapter.setSelectCount(sumSelect);
//            if (productionBeanList.isEmpty()) {
//                if (loading_layout != null) {
//                    loading_layout.setStatus(LoadingLayout.EMPTY);
//                }
//                EventBus.getDefault().post(new ControlPlusVisibleEvent(false, categoryId));
//            } else {
//                if (loading_layout != null) {
//                    loading_layout.setStatus(LoadingLayout.SUCCESS);
//                }
//                EventBus.getDefault().post(new ControlPlusVisibleEvent(true, categoryId));
//            }
////                        View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.view_empty_footer,null);
////                        recycler_view_band.getRefreshableView().addView(footerView);
////                        authorityEvaluatingAdapter.setmData(productionBeanList);
//            if (productionBeanListForServer != null) {
//                authorityEvaluatingAdapter.setmData(productionBeanListForServer);
//            }
//
//        }
//    };
//
//
//    /**
//     * 创建右滑菜单
//     */
//    SwipeMenuCreator creator = new SwipeMenuCreator() {
//        @Override
//        public void create(SwipeMenu swipeRightMenu) {
//            SwipeMenuItem addItem = new SwipeMenuItem(getActivity());
//            addItem.setBackground(R.color.color_f0514a);
//            addItem.setTitle("删除");
//            addItem.setTitleColor(Color.WHITE);
//            addItem.setWidth(DensityUtils.dip2px(getActivity(), 70));
//            addItem.setTitleSize(16);
//            swipeRightMenu.addMenuItem(addItem);
//        }
//    };
//
//    /**
//     * 右滑菜单点击取消收藏
//     */
//    private SwipeMenuListView.OnMenuItemClickListener menuItemClickListener = new SwipeMenuListView.OnMenuItemClickListener() {
//
//        @Override
//        public boolean onMenuItemClick(int adapterPosition, SwipeMenu menu, int menuPosition) {
//
////            closeable.smoothCloseMenu();// 关闭被点击的菜单。
//            //删除对比车库中的商品
//            if (productionBeanList.get(adapterPosition).isCheck()) {
////                authorityEvaluatingAdapter.selectCount = authorityEvaluatingAdapter.selectCount - 1;
////                tv_compare_percent.setText(authorityEvaluatingAdapter.selectCount + "/4");
//            }
//            ProductionBeanService.getInstance().deleteProByProId(productionBeanList.get(adapterPosition).getProId());
//            productionBeanListForServer.remove(adapterPosition);
//            if (productionBeanListForServer.isEmpty()) {
//                if (loading_layout != null) {
//                    loading_layout.setStatus(LoadingLayout.EMPTY);
//                }
//                EventBus.getDefault().post(new ControlPlusVisibleEvent(false, categoryId));
//            }
//
//            int selectCount = 0;
//            for (ProductionBean tempBean : productionBeanListForServer) {
//                if (tempBean.isCheck()) {
//                    selectCount++;
//                }
//            }
//            if (productionBeanListForServer.size() >= 1) {
//                btn_start_compare.setVisibility(View.VISIBLE);
//                if (selectCount >= 2) {
//                    btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//                    btn_start_compare.setPressedTextColor(ContextCompat.getColor(getActivity(), R.color.color_title));
//                    btn_start_compare.setOnClickListener(CompareFragment.this);
//                } else {
//                    btn_start_compare.setNormalBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//                    btn_start_compare.setPressedTextColor(ContextCompat.getColor(getActivity(), R.color.color_8e8e8e));
//                    btn_start_compare.setOnClickListener(null);
//                }
//            } else {
//                btn_start_compare.setVisibility(View.GONE);
//            }
//            authorityEvaluatingAdapter.notifyDataSetChanged();
//            return false;
//        }
//    };
//
//    /**
//     * 显示空页面
//     */
//    public void showNoProductView() {
//        productionBeanList = new ArrayList<>();
//        if (loading_layout != null) {
//            loading_layout.setStatus(LoadingLayout.EMPTY);
//        }
//        btn_start_compare.setVisibility(View.GONE);
//        EventBus.getDefault().post(new ControlPlusVisibleEvent(false, categoryId));
//    }


//    public void clearProductionEvent(ClearProductEvent clearProductEvent) {
//        if (categoryId == null) return;
//        if (categoryId == clearProductEvent.getCatId()) {
//            productionBeanList = new ArrayList<>();
//            if (productionBeanList.isEmpty()) {
//                if (loading_layout != null) {
//                    loading_layout.setStatus(LoadingLayout.EMPTY);
//                }
//                EventBus.getDefault().post(new ControlPlusVisibleEvent(false, categoryId));
//            }
//        }
//    }


    public static CompareFragment newInstance(String titleTab) {
        CompareFragment fragment = new CompareFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT_CAT_NAME, titleTab);
        fragment.setArguments(bundle);
        return fragment;
    }
//
    public static CompareFragment newInstance(String catName, String catId, String come, boolean startEnd) {
        CompareFragment fragment = new CompareFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT_CAT_NAME, catName);
        bundle.putString(ARGUMENT_CAT_ID, catId);
        bundle.putString(COME_FROM, come);
        bundle.putBoolean(ARGUMENT_START_END, startEnd);
        fragment.setArguments(bundle);
        return fragment;
    }




    @Override
    public void loadData() {
//        loadDataFromStorage();
    }


}
